package cn.agiledata.bank.linkbank.impl.chinapay;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

public class ChinapayActionInfo {
	private String ORDER_ID;
	private String FRONTEND_URL;
	private String MERCHANT_ID="821420150450083";
	private String AMOUNT;
	private String ORDER_TIME;
	private String ORDER_CURRENCY="156";
	private String CUSTOMER_IP;
	private String MALL_ID="D10103";
	private String TRANS_TYPE="01";
	public String getORDER_ID() {
		return ORDER_ID;
	}
	public void setORDER_ID(String order_id) {
		ORDER_ID = order_id;
	}
	public String getFRONTEND_URL() {
		return FRONTEND_URL;
	}
	public void setFRONTEND_URL(String frontend_url) {
		FRONTEND_URL = frontend_url;
	}
	public String getMERCHANT_ID() {
		return MERCHANT_ID;
	}
	public void setMERCHANT_ID(String merchant_id) {
		MERCHANT_ID = merchant_id;
	}
	public String getAMOUNT() {
		return AMOUNT;
	}
	public void setAMOUNT(String amount) {
		AMOUNT = amount;
	}
	public String getORDER_TIME() {
		return ORDER_TIME;
	}
	public void setORDER_TIME(String order_time) {
		ORDER_TIME = order_time;
	}
	public String getORDER_CURRENCY() {
		return ORDER_CURRENCY;
	}
	public void setORDER_CURRENCY(String order_currency) {
		ORDER_CURRENCY = order_currency;
	}
	public String getCUSTOMER_IP() {
		return CUSTOMER_IP;
	}
	public void setCUSTOMER_IP(String customer_ip) {
		CUSTOMER_IP = customer_ip;
	}
	public String getMALL_ID() {
		return MALL_ID;
	}
	public void setMALL_ID(String mall_id) {
		MALL_ID = mall_id;
	}
	public String getTRANS_TYPE() {
		return TRANS_TYPE;
	}
	public void setTRANS_TYPE(String trans_type) {
		TRANS_TYPE = trans_type;
	}
	public static String jsonRequest(String requestUrl,ChinapayActionInfo jsRequestBean) {
		String userId = "";
		try {
			// 创建连接

			URL url = new URL(requestUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");

			connection.connect();

			// POST请求
			DataOutputStream out = new DataOutputStream(connection
					.getOutputStream());
			JSONObject obj = new JSONObject();
			jsRequestBean.setORDER_ID(jsRequestBean.getORDER_ID());
			jsRequestBean.setMERCHANT_ID(jsRequestBean.getMERCHANT_ID());
			jsRequestBean.setMALL_ID(jsRequestBean.getMALL_ID());
			jsRequestBean.setAMOUNT(jsRequestBean.getAMOUNT());
			jsRequestBean.setTRANS_TYPE(jsRequestBean.getTRANS_TYPE());
			jsRequestBean.setORDER_CURRENCY(jsRequestBean.getORDER_CURRENCY());
			obj.element("ACTION_NAME", "CREATE_ORDER");
			obj.element("ACTION_INFO", jsRequestBean);
			System.out
					.println("************************请求参数开始************************");
			System.out.println(obj.toString());
			System.out
					.println("************************请求参数结束************************");
			out.writeBytes(obj.toString());
			out.flush();
			out.close();
			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			System.out
					.println("************************返回数据开始**************************");
			System.out.println(sb);
			System.out
					.println("************************返回数据结束**************************");
			reader.close();
			JSONObject jsonObj = JSONObject.fromObject((sb.toString()));
			String retCode = jsonObj.getString("ACTION_RETURN_CODE");
			System.out.println("retCode:" + retCode);
			if (retCode.equals("000000")) {
				userId = jsonObj.getString("ORDER_ID");
			}
			// 断开连接
			connection.disconnect();
		} catch (MalformedURLException e) {

			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return userId;
	}

}
